package com;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YJ
 * Date: 2024-3-10
 * Time: 16:34
 */
public class oddEvenListTest {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param head ListNode类
     * @return ListNode类
     */
    public static ListNode oddEvenList (ListNode head) {
        // write code here
        // 思路：设置两个指针，遍历两次链表，分别找出奇数/偶数
        if(head == null || head.next == null) {
            return head;
        }
        ListNode p1 = head;
        ListNode p2 = head;
        ListNode p = new ListNode(0);
        ListNode res = p;
        int count = 1;
        while(p1 != null) {
            if (count % 2 != 0) {
                p.next = p1;
                p = p.next;
            }
            p1 = p1.next;
            count++;
        }
        count = 1;
        while (p2 != null) {
            if(count % 2 == 0) {
                p.next = p2;
                p = p.next;
            }
            p2 = p2.next;
            count++;
        }
        return res.next;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode list1 = new ListNode(2);
        ListNode list2 = new ListNode(3);
        ListNode list3 = new ListNode(4);
        ListNode list4 = new ListNode(5);
        ListNode list5 = new ListNode(6);
        head.next = list1;
        list1.next = list2;
        list2.next = list3;
        list3.next = list4;
        list4.next = list5;
        oddEvenList(head);
    }
}
